package com.zhangmeng.online.exam.ui.api.model;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.zhangmeng.online.exam.ui.utils.ApiUtils;
import com.zhangmeng.online.exam.ui.api.DataLoad;
import com.zhangmeng.online.exam.ui.api.form.PermissionForm;
import com.zhangmeng.online.exam.ui.controller.PermissionEditController;
import com.zhangmeng.online.exam.ui.module.Permission;
import com.zhangmeng.online.exam.ui.service.PermissionService;
import com.zhangmeng.online.exam.ui.utils.AlertUtils;
import com.zhangmeng.online.exam.ui.utils.FxUtils;
import com.zhangmeng.online.exam.ui.utils.HttpUtils;
import javafx.beans.property.SimpleStringProperty;
import javafx.scene.Parent;
import javafx.scene.control.MenuItem;
import javafx.stage.Stage;

import java.util.*;

import static com.zhangmeng.online.exam.ui.utils.ApiUtils.API_URL;
import static com.zhangmeng.online.exam.ui.utils.FxUtils.Permission_DynamicTableComponent;

/**
 * @author zm
 * @date 2025/3/14 16:19
 * @version: 1.0
 */
public class PermissionDataLoad implements DataLoad {

    private Parent form;

    @Override
    public ApiUtils.DataView loadData(Integer pageNum, Integer pageSize) {
        Map<String, Object> params = new HashMap<>();
        params.put("pageNum", pageNum.toString());
        params.put("pageSize", pageSize.toString());
        String userListData = HttpUtils.GET(API_URL + "/permission/list",params);
        JSONObject jsonObject = JSON.parseObject(userListData);
        JSONArray data = jsonObject.getJSONArray("data");
        int total = jsonObject.getIntValue("total");

        List<Map<String, SimpleStringProperty>> permissionMapList = new ArrayList<>();
        for (Object datum : data) {
            JSONObject permission = (JSONObject) datum;
            Map<String, SimpleStringProperty> userMap = new HashMap<>();
            userMap.put("序号", new SimpleStringProperty(permission.getString("id")));
            userMap.put("id", new SimpleStringProperty(permission.getString("id")));
            userMap.put("权限名称", new SimpleStringProperty(permission.getString("name")));
            userMap.put("权限名称_name", new SimpleStringProperty(permission.getString("name")));
            userMap.put("描述", new SimpleStringProperty(permission.getString("desc")));
            userMap.put("描述_desc", new SimpleStringProperty(permission.getString("desc")));
            userMap.put("权限路径", new SimpleStringProperty(permission.getString("url")));
            userMap.put("权限路径_url", new SimpleStringProperty(permission.getString("url")));

            permissionMapList.add(userMap);
        }
        return new ApiUtils.DataView(Arrays.asList("序号", "权限名称", "描述", "权限路径"), permissionMapList).setDataLoad(this).setPageNum(pageNum).setPageSize(pageSize).setTotal(total);
    }

    @Override
    public void setForm(Parent view) {
        this.form = view;
    }

    @Override
    public Parent getForm() {
        return this.form;
    }

    @Override
    public Map<String, Object> saveForm(Map<String, Object> map) {
        return null;
    }

    @Override
    public void deleteData(String id) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        String result = HttpUtils.POST(ApiUtils.API_URL + "/permission/delete", map);
        JSONObject jsonObject = JSON.parseObject(result);
        if (jsonObject.getIntValue("code") == 200) {
            AlertUtils.alert_msg("删除成功！");
            FxUtils.FX_BEANS.get(Permission_DynamicTableComponent).flushData();
        } else {
            AlertUtils.alert_warning("删除失败！");
        }
    }

    @Override
    public void editData(String id, Stage stage) {
        PermissionForm userForm = new PermissionForm();
        PermissionEditController controller = userForm.getController();

        Permission permission = PermissionService.getPermissionById(id);
        String name = permission.getName();
        String desc = permission.getDescription();
        String url = permission.getUrl();

        controller.rolenameField.setText(name);
        controller.descriptionField.setText(desc);
        controller.urlField.setText(url);
        controller.permissionComboBox.setValue(permission);
        controller.setEditPermission(permission);
        AlertUtils.alert("编辑权限信息", (Parent) userForm, stage);
    }


    @Override
    public void updateData(String id, String name, String oldValue, String newValue) {

    }

    @Override
    public List<MenuItem> getContextMenu() {
        return null;
    }

    @Override
    public ApiUtils.DataView searchData(Map<String, Object> searchText, int pageNum, int pageSize) {
        return null;
    }
}
